package com.junxonline.instant.util;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 跨域支持工具类
 *
 * @author JunX
 * @date 2021-06-27
 */
public class CorsUtils {

    /**
     * 为Response设置跨域支持
     * @param request request
     * @param response response
     * @return HttpServletResponse
     */
    public static HttpServletResponse setResponseCorsSupport(ServletRequest request, ServletResponse response) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Resquest-Headers"));
        return httpServletResponse;
    }

}
